// pages/marketOrder/my.js
const app = getApp();
const network = require("../../utils/network");
const util = require("../../utils/util");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        requestUrl: app.api.marketOrderMy,
        requestParams: {
            type: 1
        },
        searchForm: {
            customer_id: '',
            contract_num: '',
            customer_name: null
        },
        fabs: [{
            iconPath: 'https://oa-meilai.obs.cn-jssz1.ctyun.cn/20221102145321081195.png',
            text: '搜索'
        }, {
            iconPath: 'https://oa-meilai.obs.cn-jssz1.ctyun.cn/20221102145517219958.png',
            text: '刷新'
        }],
        customer_arr: [],
        selectCustomerLoading: true
    },
    toPage_left2({detail: {id}}) {
        wx.navigateTo({
            url: '/pages/marketOrder/detail?id=' + id
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.initCustomer()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.selectComponent('.market-order-list-component').handleReachBottom()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this.selectComponent('.market-order-list-component').handlePullDownRefresh()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    // todo
    inputChange(e) {
        this.setData({
            [e.target.dataset.key]: e.detail
        })
    },
    resetSearch() {
        this.setData({
            searchForm: {
                customer_id: '',
                contract_num: '',
                customer_name: '',
                type: 1
            }
        });
    },
    onSearch() {
        const _this = this;
        const _params = JSON.parse(JSON.stringify(_this.data.searchForm || {}));
        let params = {};
        if (!!_params) {
            const keys = Object.keys(_params) || [];
            keys.forEach(key => {
                if (_params[key] != null && _params[key] != undefined) params[key] = _params[key];
            });
        }
        _this.setData({
            requestParams: Object.assign({}, _this.data.searchForm, params)
        }, function () {
            _this.selectComponent('.market-order-list-component').handlePullDownRefresh()
            _this.selectComponent('.search-box').close();
        })
    },
    onReset() {
        const _this = this;
        _this.resetSearch();
        _this.setData({
            requestParams: Object.assign({}, _this.data.searchForm)
        }, function () {
            _this.selectComponent('.market-order-list-component').handlePullDownRefresh()
            _this.selectComponent('.search-box').close();
        })
    },
    initCustomer() {
        const that = this
        util.showLoading('加载中')
        network.reqPromise('GET', {
            uri: app.api.customerList,
            params: {}
        }).then(res => {
            that.setData({
                customer_arr: res.data,
                selectCustomerLoading: false
            })
            util.hideLoading()
        }).catch(() => {
            util.hideLoading()
        })
    },
    clickFab({
                 detail
             }) {
        const _this = this
        if (detail === '刷新') {
            _this.resetSearch();
            _this.setData({
                searchForm: Object.assign({}, _this.data.defaultRequestParams)
            }, function () {
                _this.selectComponent('.market-order-list-component').handlePullDownRefresh()
            })
        }
        if (detail === '搜索') {
            _this.selectComponent('.search-box').show();
        }
    },
    selectCustomerConfirm(e) {
        this.setData({
            'searchForm.customer_id': e.detail.value.id,
            'searchForm.customer_name': e.detail.value.name,
        })
    }
})
